package com.wxzz.elearing.livetelecast.amqp.producer;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.livetelecast.entity.Activity;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecast;
import com.wxzz.elearing.livetelecast.mapper.ActivityMapper;
import com.wxzz.elearing.livetelecast.mapper.PlatformLivetelecastMapper;
import com.wxzz.elearning.common.bean.RabbitMQConfiguration;
import com.wxzz.elearning.common.constants.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.UUID;

@Component
@Slf4j
public class PlatformLivetelecastProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 延迟修改活动开始状态
     *
     * @param activityId
     * @param startTime
     */
    public void updateActivityStartStatus(Long activityId, LocalDateTime startTime) {
        String uuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE6;
        Integer status = 1;
        JSONObject res = new JSONObject();
        res.put("uuid", uuid);
        res.put("id", activityId);
        res.put("type", RabbitMQConfiguration.QUEUE_TYPE6);
        res.put("status", status);
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + activityId + RabbitMQConfiguration.QUEUE_TYPE6 + status, uuid);
        log.info("（延迟修改活动状态1）DeadLetterSender 发送时间: {} msg内容： {}", LocalDateTime.now().toString(), res.toJSONString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                res.toJSONString(),
                (msg) -> {
                    Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    Long start = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    log.info("当前需插入的队列任务时差:{}", (start - now) / 1000);
                    msg.getMessageProperties().setDelay((int) (start - now));
                    return msg;
                });

        String willUuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE9;
        JSONObject willRes = new JSONObject();
        willRes.put("uuid", willUuid);
        willRes.put("id", activityId);
        willRes.put("type", RabbitMQConfiguration.QUEUE_TYPE9);
        willRes.put("status", 0);
        //查询活动
        Activity activity = activityMapper.selectById(activityId);
        //提前发送消息
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                willRes.toJSONString(),
                (msg) -> {
                    Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    Long start = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    log.info("当前需插入的队列任务时差:{}", (start - now) / 1000);
                    if ((start - now - activity.getSmsRemindMinute() * 1000 * 60) < 0) {
                        msg.getMessageProperties().setDelay(0);
                    } else {
                        msg.getMessageProperties().setDelay((int) (start - now - activity.getSmsRemindMinute() * 1000 * 60));
                    }
                    return msg;
                });
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + activity + RabbitMQConfiguration.QUEUE_TYPE9 + 0, willUuid);
    }

    /**
     * 延迟修改活动结束状态
     *
     * @param activityId
     */
    public void updateActivityEndStatus(Long activityId, LocalDateTime startTime) {
        String uuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE7;
        Integer status = 2;
        JSONObject res = new JSONObject();
        res.put("uuid", uuid);
        res.put("id", activityId);
        res.put("type", RabbitMQConfiguration.QUEUE_TYPE7);
        res.put("status", status);
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + activityId + RabbitMQConfiguration.QUEUE_TYPE7 + status, uuid);
        log.info("（延迟修改活动状态2）DeadLetterSender 发送时间: {} msg内容： {}", LocalDateTime.now().toString(), res.toJSONString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                res.toJSONString(),
                (msg) -> {
                    Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    Long start = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    log.info("当前需插入的队列任务时差:{}", (start - now) / 1000);
                    msg.getMessageProperties().setDelay((int) (start - now));
                    return msg;
                });
    }


    @Autowired
    private PlatformLivetelecastMapper platformLivetelecastMapper;

    /**
     * 延迟修改直播间状态
     *
     * @param platformLivetelecastId
     * @param startTime
     */
    public void updatePlatformLivetelecastStatus(Long platformLivetelecastId, LocalDateTime startTime) {
        String uuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE1;
        Integer liveStatus = new Integer(1);
        JSONObject res = new JSONObject();
        res.put("uuid", uuid);
        res.put("id", platformLivetelecastId);
        res.put("type", RabbitMQConfiguration.QUEUE_TYPE1);
        res.put("status", liveStatus);
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + platformLivetelecastId + RabbitMQConfiguration.QUEUE_TYPE1 + liveStatus, uuid);
        log.info("（延迟修改直播间状态）DeadLetterSender 发送时间: {} msg内容： {}", LocalDateTime.now().toString(), res.toJSONString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                res.toJSONString(),
                (msg) -> {
                    Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    Long start = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    log.info("当前需插入的队列任务时差:{}", (start - now) / 1000);
                    msg.getMessageProperties().setDelay((int) (start - now));
                    return msg;
                });
        String willUuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE3;
        JSONObject willRes = new JSONObject();
        willRes.put("uuid", willUuid);
        willRes.put("id", platformLivetelecastId);
        willRes.put("type", RabbitMQConfiguration.QUEUE_TYPE3);
        willRes.put("status", 0);
        //查询直播
        PlatformLivetelecast platformLivetelecast = platformLivetelecastMapper.selectById(platformLivetelecastId);
        //提前发送消息
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                willRes.toJSONString(),
                (msg) -> {
                    Long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    Long start = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    log.info("当前需插入的队列任务时差:{}", (start - now) / 1000);
                    if ((start - now - platformLivetelecast.getSmsRemindMinute() * 1000 * 60) < 0) {
                        msg.getMessageProperties().setDelay(0);
                    } else {
                        msg.getMessageProperties().setDelay((int) (start - now - platformLivetelecast.getSmsRemindMinute() * 1000 * 60));
                    }
                    return msg;
                });
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + platformLivetelecastId + RabbitMQConfiguration.QUEUE_TYPE3 + 0, willUuid);
    }

    /**
     * 延迟修改直播推流状态
     *
     * @param platformLivetelecastId
     * @param seconds                延迟秒数（s）
     */
    public void updatePushStatus(Long platformLivetelecastId, Integer pushStatus, Integer seconds) {
        String uuid = UUID.randomUUID().toString() + RabbitMQConfiguration.QUEUE_TYPE2;
        JSONObject res = new JSONObject();
        res.put("uuid", uuid);
        res.put("id", platformLivetelecastId);
        res.put("type", RabbitMQConfiguration.QUEUE_TYPE2);
        res.put("status", pushStatus);
        log.info("（延迟修改直播推流状态）DeadLetterSender 发送时间: {} msg内容： {}", LocalDateTime.now().toString(), res.toJSONString());
        rabbitTemplate.convertAndSend(
                RabbitMQConfiguration.WXZZ_DELAY_EXCHANGE,
                RabbitMQConfiguration.WXZZ_DELAY_KEY,
                res.toJSONString(),
                (msg) -> {
                    msg.getMessageProperties().setDelay(seconds * 1000);
                    return msg;
                });
        //保持消息的唯一
        redisTemplate.opsForValue().set(RedisConstants.DEAL_QUEUE_LIVETELECAST + platformLivetelecastId + RabbitMQConfiguration.QUEUE_TYPE2 + pushStatus, uuid);
    }

}
